package com.daxia.bumall.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.daxia.bumall.dao.ApiTestParameterDAO;
import com.daxia.bumall.dto.ApiTestParameterDTO;
import com.daxia.bumall.model.ApiTestParameter;
import com.daxia.bumall.support.Page;
import com.daxia.bumall.util.BeanMapper;

@Service
public class ApiTestParameterService {
	
	@Autowired
	private ApiTestParameterDAO apiTestParameterDAO;
	
	public List<ApiTestParameterDTO> find(ApiTestParameterDTO query, Page page) {
		List<ApiTestParameter> models = apiTestParameterDAO.find(query, page);
		List<ApiTestParameterDTO> dtos = toDTOs(models);
		return dtos;
	}
	
	public List<ApiTestParameterDTO> findAll() {
		return this.find(new ApiTestParameterDTO(), null);
	}
	
	public List<ApiTestParameterDTO> find(int count) {
		Page page = new Page();
		page.setPage(1);
		page.setPageSize(count);
		return this.find(new ApiTestParameterDTO(), page);
	}
	
	public Long create(ApiTestParameterDTO dto) {
		ApiTestParameter model = new ApiTestParameter();
		toModel(model, dto);
		return apiTestParameterDAO.create(model);
	}
	
	public ApiTestParameterDTO load(Long id) {
	    ApiTestParameter model = apiTestParameterDAO.load(id);
	    return toDTO(model);
    }

	public void updateAllFields(ApiTestParameterDTO dto) {
		ApiTestParameter model = apiTestParameterDAO.load(dto.getId());
		toModel(model, dto);
		apiTestParameterDAO.update(model);
    }

	public void deleteByIds(Long[] ids) {
		if (ids != null) {
			for (Long id : ids) {
				apiTestParameterDAO.deleteById(id);
			}
		}
    }
	
	public void deleteById(Long id) {
		this.deleteByIds(new Long[] {id});
	}

	public ApiTestParameterDTO findOne(ApiTestParameterDTO query) {
		ApiTestParameter model = apiTestParameterDAO.findOne(query);
		return toDTO(model);
	}
	
	private List<ApiTestParameterDTO> toDTOs(List<ApiTestParameter> models) {
		if (CollectionUtils.isEmpty(models)) {
			return new ArrayList<ApiTestParameterDTO>(0);
		}
		List<ApiTestParameterDTO> dtos = new ArrayList<ApiTestParameterDTO>(models.size());
		for (ApiTestParameter model : models) {
	        ApiTestParameterDTO dto = toDTO(model);
	        dtos.add(dto);
        }
	    return dtos;
    }
    
    private ApiTestParameterDTO toDTO(ApiTestParameter model) {
		if (model == null) {
			return null;
		}
		ApiTestParameterDTO dto = BeanMapper.map(model, ApiTestParameterDTO.class);
		
		return dto;
	}
	
	private void toModel(ApiTestParameter model, ApiTestParameterDTO dto) {
		BeanMapper.copy(dto, model);
    }
	
	@SuppressWarnings("unused")
	private List<ApiTestParameter> toModels(List<ApiTestParameterDTO> dtos) {
		if (CollectionUtils.isEmpty(dtos)) {
			return new ArrayList<ApiTestParameter>(0);
		}
		List<ApiTestParameter> models = new ArrayList<ApiTestParameter>(dtos.size());
		for (ApiTestParameterDTO dto : dtos) {
	        ApiTestParameter model = new ApiTestParameter();
	        toModel(model, dto);
	        models.add(model);
        }
		return models;
	}
}
